package level.editor.tool.layer;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import core.Constants;

public class ToolLayerBlock extends JPanel implements ActionListener,
		MouseListener, FocusListener
{
	private String name;
	private JPanel namePanel;
	private JTextField textField;
	private JLabel label;
	private int zOrder;
	private boolean isVisible;
	private boolean isLocked;

	public ToolLayerBlock(String name, int zOrder)
	{
		super();
		this.name = name;
		this.zOrder = zOrder;
		isVisible = true;
		isLocked = false;
		namePanel = null;
		textField = null;
		label = null;
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		setMaximumSize(new Dimension(400, 30));
		setBorder(BorderFactory.createRaisedBevelBorder());
		setBackground(new Color(248, 248, 248));
		setAlignmentX(Component.CENTER_ALIGNMENT);
		createDeleteButton();
		createEditNameButton();
		createNameField();
		createModButtons();
		createArrows();
		setFocusable(true);
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getActionCommand().equals("Delete"))
			System.out
					.println("Implement the delete layer function you retard");
		if (e.getActionCommand().equals("Edit"))
		{
			if (!textField.isVisible())
			{
				CardLayout layout = (CardLayout) namePanel.getLayout();
				layout.show(namePanel, "Field");
				textField.setCaretPosition(textField.getText().length());
				textField.requestFocusInWindow();
				textField.selectAll();
			}
			else if (textField.isVisible())
			{
				changeName();
			}
		}
		if (e.getActionCommand().equals("Field"))
		{
			changeName();
		}
		if (e.getActionCommand().equals("Vis"))
		{
			if (e.getSource() instanceof JButton)
			{
				System.out.println("SUP");
				JButton button = (JButton) e.getSource();
				button.setIcon((!isVisible ? new ImageIcon(Constants.ICON_DIR
						+ "/eye_open.png") : new ImageIcon(Constants.ICON_DIR
						+ "/eye_close.png")));
				isVisible = !isVisible;
			}
		}
		if (e.getActionCommand().equals("Lock"))
		{
			if (e.getSource() instanceof JButton)
			{
				JButton button = (JButton) e.getSource();
				button.setIcon((!isLocked ? new ImageIcon(Constants.ICON_DIR
						+ "/lock_close.png") : new ImageIcon(Constants.ICON_DIR
						+ "/lock_open.png")));
				isLocked = !isLocked;
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{
		CardLayout layout = (CardLayout) namePanel.getLayout();
		layout.show(namePanel, "Field");
		textField.setCaretPosition(textField.getText().length());
		textField.requestFocusInWindow();
		textField.selectAll();
	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e)
	{

	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void focusGained(FocusEvent e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void focusLost(FocusEvent e)
	{
		CardLayout layout = (CardLayout) namePanel.getLayout();
		textField.setText(label.getText());
		layout.show(namePanel, "Label");
	}

	private void createDeleteButton()
	{
		JButton delete = new JButton(new ImageIcon(Constants.ICON_DIR
				+ "/delete.png"));

		delete.setAlignmentY(CENTER_ALIGNMENT);
		delete.setMaximumSize(new Dimension(23, 23));
		delete.setPreferredSize(new Dimension(23, 23));
		delete.setMinimumSize(new Dimension(23, 23));
		delete.setContentAreaFilled(false);
		delete.setActionCommand("Delete");
		delete.setFocusPainted(false);
		delete.addActionListener(this);
		add(delete);
		add(Box.createRigidArea(new Dimension(15, 0)));
	}

	private void createEditNameButton()
	{
		JButton edit = new JButton(new ImageIcon(Constants.ICON_DIR
				+ "/edit.png"));

		edit.setAlignmentY(CENTER_ALIGNMENT);
		edit.setMaximumSize(new Dimension(23, 23));
		edit.setMinimumSize(new Dimension(23, 23));
		edit.setContentAreaFilled(false);
		edit.setActionCommand("Edit");
		edit.setFocusable(false);
		edit.addActionListener(this);
		add(edit);
	}

	private void createNameField()
	{
		namePanel = new JPanel(new CardLayout());
		JPanel labelPanel = new JPanel();
		labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.LINE_AXIS));
		label = new JLabel(name);
		label.setBackground(new Color(248, 248, 248));
		labelPanel.addMouseListener(this);
		textField = new JTextField(name, 30);
		textField.setBackground(Color.white);
		textField.addActionListener(this);
		textField.setActionCommand("Field");
		textField.addFocusListener(this);
		labelPanel.add(Box.createRigidArea(new Dimension(3, 0)));
		labelPanel.add(label);
		namePanel.add(labelPanel, "Label");
		namePanel.add(textField, "Field");
		namePanel.setAlignmentY(CENTER_ALIGNMENT);
		namePanel.setMinimumSize(new Dimension(120, 20));
		namePanel.setMaximumSize(new Dimension(120, 20));
		namePanel.setPreferredSize(new Dimension(120, 20));
		namePanel.setBorder(BorderFactory.createLineBorder(Color.gray));
		add(namePanel);
	}

	private void createModButtons()
	{
		JButton vis = new JButton((isVisible ? new ImageIcon(Constants.ICON_DIR
				+ "/eye_open.png") : new ImageIcon(Constants.ICON_DIR
				+ "/eye_close.png")));
		vis.addActionListener(this);
		vis.setAlignmentY(CENTER_ALIGNMENT);
		vis.setBackground(new Color(248, 248, 248));
		vis.setMaximumSize(new Dimension(23, 23));
		vis.setMinimumSize(new Dimension(23, 23));
		vis.setPreferredSize(new Dimension(23, 23));
		vis.setContentAreaFilled(false);
		vis.setActionCommand("Vis");
		vis.setFocusable(false);


		JButton lock = new JButton((isLocked ? new ImageIcon(Constants.ICON_DIR
				+ "/lock_close.png") : new ImageIcon(Constants.ICON_DIR
				+ "/lock_open.png")));
		lock.addActionListener(this);
		lock.setAlignmentY(CENTER_ALIGNMENT);
		lock.setBackground(new Color(248, 248, 248));
		lock.setMaximumSize(new Dimension(23, 23));
		lock.setMinimumSize(new Dimension(23, 23));
		lock.setPreferredSize(new Dimension(23, 23));
		lock.setContentAreaFilled(false);
		lock.setActionCommand("Lock");
		lock.setFocusable(false);

		add(Box.createRigidArea(new Dimension(35, 0)));
		add(vis);
		add(Box.createRigidArea(new Dimension(30, 0)));
		add(lock);
		add(Box.createRigidArea(new Dimension(30, 0)));
	}

	private void createArrows()
	{
		JButton up = new JButton(new ImageIcon(Constants.ICON_DIR
				+ "/up_arrow.png"));

		up.setAlignmentY(CENTER_ALIGNMENT);
		up.setMaximumSize(new Dimension(23, 23));
		up.setPreferredSize(new Dimension(23, 23));
		up.setMinimumSize(new Dimension(23, 23));
		up.setActionCommand("Up");
		up.setContentAreaFilled(false);
		up.setFocusPainted(false);
		up.addActionListener(this);

		JButton down = new JButton(new ImageIcon(Constants.ICON_DIR
				+ "/down_arrow.png"));

		down.setAlignmentY(CENTER_ALIGNMENT);
		down.setMaximumSize(new Dimension(23, 23));
		down.setPreferredSize(new Dimension(23, 23));
		down.setMinimumSize(new Dimension(23, 23));
		down.setActionCommand("Down");
		down.setContentAreaFilled(false);
		down.setFocusPainted(false);
		down.addActionListener(this);
		add(up);
		add(down);
	}

	private void changeName()
	{
		CardLayout layout = (CardLayout) namePanel.getLayout();
		// Ask ToolLayer for validity here

		name = textField.getText();
		label.setText(name);
		layout.show(namePanel, "Label");
	}
}
